package com.teng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.teng.entity.RecycleEntity;
import com.teng.entity.RoleEntity;
import com.teng.exception.OAException;
import com.teng.util.DBUtil;

public class RecycleDao {
		public List<RecycleEntity> recyclelist(String id)throws Exception{
			Connection connection=DBUtil.getConnection();
			List<RecycleEntity> re=new ArrayList<>();
			try {
				StringBuilder sql=new StringBuilder();
				sql.append(" select doc.* from ")
					.append(" doc_document_attachment as doc INNER JOIN org_department as de ")
					.append(" on doc.department_id=de.id") 
					.append(" INNER JOIN sys_user as us on us.id=doc.create_emp_id")
					.append(" where ")
					.append(" us.id=?")
					.append(" and is_deleted=1");
						
				PreparedStatement ps=connection.prepareStatement(sql.toString());
				ps.setString(1, id);
				ResultSet rs=ps.executeQuery();
				
				while(rs.next()) {

					re.add(deposit(rs));
				}
				return re;
			}finally{
				connection.close();
			}
}
		
		
		public List<RecycleEntity> recyclecheck(String id)throws Exception{
			Connection connection=DBUtil.getConnection();
			List<RecycleEntity> re=new ArrayList<>();
			try {
				StringBuilder sql=new StringBuilder();
				sql.append(" select * from ")
					.append(" doc_document_attachment")
					.append(" where id=?");
						
				PreparedStatement ps=connection.prepareStatement(sql.toString());
				ps.setString(1, id);
				ResultSet rs=ps.executeQuery();
				if(rs.next()) {
					re.add(deposit(rs));
				}
				return re;
			}finally {
				connection.close();
			}
		}
		
		public RecycleEntity deposit(ResultSet rs) throws Exception{
			RecycleEntity e = new RecycleEntity();
			e.setName(rs.getString("name"));
			e.setIsDirectory(rs.getInt("is_directory"));
			e.setParentId(rs.getString("parent_id"));
			e.setDepartmentId(rs.getString("department_id"));
			e.setCreateTime(rs.getTimestamp("create_time"));
			e.setSavePath(rs.getString("save_path"));
			e.setCreateEmpId(rs.getString("create_emp_id"));
			e.setId(rs.getString("id"));
			e.setIsDeleted(rs.getInt("is_deleted"));
			e.setAttchmentCount(rs.getInt("attchment_count"));
			return e;
		}
}
